<?php


namespace app\reception\logic;


use app\common\model\QuickApp;
use app\common\model\SignConfig;
use app\common\model\TaskConfig;
use app\common\model\UserBookPoints;
use think\Db;

class TaskLogic
{

    /**
     * 签到模板
     * @param $qa
     * @param $agent_id
     * @return array|\int[][]
     */
    public function signTemplate($qa, $agent_id)
    {
        $where[] = ['quick_id', '=', $qa->id];
        $where[] = ['manage_id', '=', $agent_id];
        $defaultTemplate = SignConfig::defaultTemplate($qa->info->sign_max);
        $mySignData = SignConfig::where($where)->value('sign_data');
        $mySignData = json_decode($mySignData, true);
        $data = [];
        if ($mySignData) {
            foreach ($defaultTemplate as $k => $v) {
                if (isset($mySignData[$k])) {
                    $data[$k] = $mySignData[$k];
                } else {
                    $data[$k] = $v;
                }
            }
        } else {
            $data = $defaultTemplate;
        }
        return $data;
    }


    /**
     * 获取任务书点值
     * @param $key
     * @param $user
     * @return int|mixed
     */
    public static function getTaskPoint($key, $user)
    {
        $where[] = ['quick_id', '=', $user->quick_id];
        if ($user->agent_id) {
            $where[] = ['manage_id', '=', $user->agent_id];
        } else {
            $where[] = ['manage_id', '=', 0];
        }
        $myTaskData = TaskConfig::where($where)->value('task_data');
        $defaultTemplate = TaskConfig::defaultTemplate($user->quick_id);
        $myTaskData = json_decode($myTaskData, true);
        $data = [];
        if ($myTaskData) {
            foreach ($defaultTemplate as $k => $v) {
                if (isset($myTaskData[$k])) {
                    $data[$k] = $myTaskData[$k];
                } else {
                    $data[$k] = $v;
                }
            }
        } else {
            $data = $defaultTemplate;
        }
        $point = 0;
        foreach ($data as $k => $v) {
            if ($k == $key) {
                $point = $v['point'];
            }
        }
        return $point;
    }
}